package org.jsola.hr.service.impl;

import org.jsola.cache.ICache;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.CacheKeyKit;
import org.jsola.hr.dto.EmpTagDTO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.service.ICompanyService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.service.IEmpTagService;
import org.jsola.hr.vo.CompanyListVO;
import org.jsola.hr.vo.EmpTagVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service("hrEmpTagServiceImpl")
public class EmpTagServiceImpl implements IEmpTagService {

    @Autowired
    private ICache cache;

    @Autowired
    private IEmpInfoService empInfoService;

    @Autowired
    private ICompanyService companyService;

    @Override
    public List<EmpTagVO> listByCompanyId(String companyId, TokenUser tokenUser) {
        String siteId = tokenUser.getSiteId();
        String key = CacheKeyKit.getTag(siteId, companyId);
        List<EmpTagVO> list = cache.get(key);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    @Override
    public void saveTagByCompanyId(String companyId, String tag, TokenUser tokenUser) {
        List<EmpTagVO> empTagVOS = listByCompanyId(companyId, tokenUser);
        List<String> tagList = empTagVOS.stream()
                .map(EmpTagVO::getTagName)
                .collect(Collectors.toList());
        if (!tagList.contains(tag)) {
            EmpTagDTO empTagDTO = new EmpTagDTO();
            empTagDTO.setCompanyId(companyId);
            empTagDTO.setTagName(tag);
            save(empTagDTO, tokenUser);
        }
    }

    @Override
    public List<EmpTagVO> listBySiteId(TokenUser tokenUser) {
        // 查询该租户下面的所有用工单位
        List<CompanyListVO> companyListVOList = companyService.selectCompanyList(tokenUser);
        // 查询该用工单位下面的标签
        List<EmpTagVO> empTagVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(companyListVOList)) {
            for (CompanyListVO companyListVO : companyListVOList) {
                List<EmpTagVO> empTagList = this.listByCompanyId(companyListVO.getId(), tokenUser);
                if (!CollectionUtils.isEmpty(empTagList)) {
                    empTagVOList.addAll(empTagList);
                }
            }
        }
        return empTagVOList;
    }

    @Override
    public EmpTagVO save(EmpTagDTO empTagDTO, TokenUser tokenUser) {
        String siteId = tokenUser.getSiteId();
        String companyId = empTagDTO.getCompanyId();
        String tagName = empTagDTO.getTagName();
        String key = CacheKeyKit.getTag(siteId, companyId);
        List<EmpTagVO> list = new ArrayList<>();
        List<EmpTagVO> list1 = cache.get(key);
        if (!CollectionUtils.isEmpty(list1)) {
            for (EmpTagVO empTagVO : list1) {
                if (tagName.equals(empTagVO.getTagName())) {
                    throw new ParamException("标签【" + tagName + "】已存在");
                }
            }
            list.addAll(list1);
        }
        EmpTagVO empTagVO = empTagDTO.to(EmpTagVO.class);
        list.add(empTagVO);
        cache.set(key, list);
        return empTagVO;
    }

    @Override
    public int delete(TokenUser tokenUser, EmpTagDTO empTagDTO) {

        String siteId = tokenUser.getSiteId();
        String companyId = empTagDTO.getCompanyId();
        String tagName = empTagDTO.getTagName();
        // 此标签是否有员工使用
        List<EmpInfoDO> empInfoDOList = empInfoService.selectByTags(companyId, siteId, Collections.singletonList(tagName));
        if (!CollectionUtils.isEmpty(empInfoDOList)) {
            throw new ParamException("此标签有员工正在使用,不允许删除");
        }
        String key = CacheKeyKit.getTag(siteId, companyId);
        List<EmpTagVO> list = cache.get(key);
        int count = 0;
        if (!CollectionUtils.isEmpty(list)) {
            Iterator<EmpTagVO> iterator = list.iterator();
            while (iterator.hasNext()) {
                EmpTagVO empTagVO = iterator.next();
                if (Objects.nonNull(empTagVO)) {
                    if (tagName.equals(empTagVO.getTagName())) {
                        iterator.remove();
                        count++;
                        break;
                    }
                }
            }
            // 重写缓存数据
            cache.set(key, list);
        }
        return count;
    }
}
